import mongoose from 'mongoose'
const { ErrorHandler } = require('./common/errorHandler')
const OnlineProducts = mongoose.model('OnlineProducts')
mongoose.set('useFindAndModify', false)
const { LocalRequestAllowed } = require('./common/localRequestAllowed')
const { PageChecker } = require('./common/page')


export const getContentList = async (ctx) => {
  try {
    await LocalRequestAllowed(ctx).then(async () => { // 只允许本地请求
      const { pageNum, pageSize, type } = ctx.request.body
      await PageChecker(pageSize, pageNum) // 分页校验
      if(type){
        const conditon = { online: true, type } // 查询条件
        const total = await OnlineProducts.find(conditon).count().exec();
        const data = await OnlineProducts.find(conditon)
            .skip((Number(pageNum) - 1) * Number(pageSize))
            .limit(Number(pageSize))   
            .sort({'_id':-1})
            .exec()
        ctx.body = {
          success: true,
          data,
          total,
          pageNum,
          pageSize
        }
      } else {
        ctx.body = {
          success: false,
          data: 'type未传'
        }
      }
    })
  } catch (e) {
    ErrorHandler(e, ctx)
  }
}

export const getContentById = async (ctx) => {
  try {
    await LocalRequestAllowed(ctx).then(async () => { // 只允许本地请求
      const { id } = ctx.request.body
      var _id = mongoose.Types.ObjectId(id);
      if(id){
        const conditon = { _id } // 查询条件
        const data = await OnlineProducts.findOne(conditon)
            .select()
            .exec()
        ctx.body = {
          success: true,
          data,
        }
      } else {
        ctx.body = {
          success: false,
          data: 'id未传'
        }
      }
    })
  } catch (e) {
    ErrorHandler(e, ctx)
  }
}

export const contentSearch = async (ctx) => {
  try {
    await LocalRequestAllowed(ctx).then(async () => { // 只允许本地请求
      const { pageNum, pageSize, keywords } = ctx.request.body
      if(keywords){
        const conditon = { name: new RegExp(keywords) } // 查询条件
        const total = await OnlineProducts.find(conditon).count().exec();
        const data = await OnlineProducts.find(conditon)
            .skip((Number(pageNum) - 1) * Number(pageSize))
            .limit(Number(pageSize))   
            .sort({'_id':-1})
            .exec()
        // const data = await OnlineProducts.findOne(conditon)
        //     .select()
        //     .exec()
        ctx.body = {
          success: true,
          data,
          total,
          pageNum,
          pageSize
        }
      } else {
        ctx.body = {
          success: false,
          data: 'keywords未传'
        }
      }
    })
  } catch (e) {
    ErrorHandler(e, ctx)
  }
}

export const publishContent = async (ctx) => {
  try {
    await LocalRequestAllowed(ctx).then(async () => { // 只允许本地请求

      await PageChecker(pageSize, pageNum) // 分页校验
      if(type){
        const conditon = { online: true, type } // 查询条件

        await Users.create({ name, describe, type, contentType, previewImagePath, content, commentId,  })
       
  //       name: String,
  // describe: String,
  // type: Number, 
  // contentType: Number,
  // previewImagePath: String,
  // content: String,
  // commentId: Number,
  // publisherId: Number,
  // likeCount: Number,
  // favCount: Number,
  // hotSearchFlag: Boolean,
  // online: Boolean,
  // createTime: {
  //   type: String,
  //   default: utils.currentTimeStamp()
  // }
        const data = await OnlineProducts.find(conditon)
            .skip((Number(pageNum) - 1) * Number(pageSize))
            .limit(Number(pageSize))   
            .sort({'_id':-1})
            .exec()
        ctx.body = {
          success: true,
          data,
          total,
          pageNum,
          pageSize
        }
      } else {
        ctx.body = {
          success: false,
          data: 'type未传'
        }
      }
    })
  } catch (e) {
    ErrorHandler(e, ctx)
  }
}


